package by.parfen.messages.webapp;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import by.parfen.messages.webapp.receive.ReceiveMessagePage;
import by.parfen.messages.webapp.sendmessage.SendMessagePage;

public abstract class BaseLayout extends WebPage {

	private static final String MENU_TEXT = "menuText";
	private static final String MENU_LINK = "menuLink";

	private static final String P_MENU_SEND = "menu.SendMessagePage";
	private static final String P_MENU_RECEIVE = "menu.ReceiveMessagePage";

	private String currentMenuTitle;

	public String getCurrentMenuTitle() {
		return currentMenuTitle;
	}

	public void setCurrentMenuTitle(String currentMenuTitle) {
		this.currentMenuTitle = currentMenuTitle;
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		add(new Label("headerTitle", getPageTitle()));

		createNavigationMenu();

		add(new FeedbackPanel("feedbackpanel"));
	}

	private void applyMenuAttrib(final WebMarkupContainer menuItem, String menuTitle) {
		if (currentMenuTitle == menuTitle) {
			menuItem.add(new AttributeModifier("class", new Model<String>("current")));
		}
	}

	private void createNavigationMenu() {
		RepeatingView menuLinkList;
		WebMarkupContainer menuItem;
		Link<?> link;

		menuLinkList = new RepeatingView("menuLinkList");
		add(menuLinkList);

		// #1 Send menu element
		menuItem = new WebMarkupContainer(menuLinkList.newChildId());
		link = new Link<Void>(MENU_LINK) {
			@Override
			public void onClick() {
				setResponsePage(SendMessagePage.class);
			}
		};
		link.add(new Label(MENU_TEXT, new ResourceModel(P_MENU_SEND)));
		applyMenuAttrib(menuItem, P_MENU_SEND);
		menuItem.add(link);
		menuLinkList.add(menuItem);

		// #2 Receive menu element
		menuItem = new WebMarkupContainer(menuLinkList.newChildId());
		link = new Link<Void>(MENU_LINK) {
			@Override
			public void onClick() {
				setResponsePage(ReceiveMessagePage.class);
			}
		};
		link.add(new Label(MENU_TEXT, new ResourceModel(P_MENU_RECEIVE)));
		applyMenuAttrib(menuItem, P_MENU_RECEIVE);
		menuItem.add(link);
		menuLinkList.add(menuItem);

		// #8 Menu element
		menuItem = new WebMarkupContainer(menuLinkList.newChildId());
		link = new Link<Void>(MENU_LINK) {
			@Override
			public void onClick() {
				// setResponsePage(AutoComplitePage.class);
			}
		};
		link.add(new Label(MENU_TEXT, new Model<String>("***")));
		applyMenuAttrib(menuItem, "***");
		menuItem.add(link);
		menuLinkList.add(menuItem);

	}

	protected IModel<String> getPageTitle() {
		return new Model<String>(getClass().getSimpleName());
	}
}
